﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using dotNET.Application.Infrastructure;
using dotNET.Data;
using dotNET.Domain.Entities;
using dotNET.Repository;
using dotNET.Dto;
using dotNET.Utility.Cache;
namespace dotNET.Application.App
{
    public class ItemsDataApp : App, IItemsDataApp
    {
        public IRepositoryBase<ItemsData> _ItemsDataRep { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="isSaas"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        public async Task<List<ItemsData>> GetListAsync( ItemsDataOption option)
        {
            Sql sql = Sql.Builder;
            if(option.ParentId==0)
            {
                return new List<ItemsData>();
            }
                sql = sql.Where("ParentId=@ParentId", new { ParentId = option.ParentId });
            
            if (option != null)
            {
                if (!string.IsNullOrEmpty(option.Name))
                {
                    option.Name = "%"+option.Name+"%";
                    sql = sql.Where("`Name` like @Name", new { Name = option.Name });
                }
               
            }
           
            var t= (await _ItemsDataRep.GetListAsync(sql.SQL, sql.Arguments)).ToList();

            return t;
        }

        /// <summary>
        /// 菜单
        /// </summary>
        /// <param name="isSaas"></param>
        /// <returns></returns>
        public async Task<List<ItemsData>> GetItemsDataListAsync()
        {

            var r = await _ItemsDataRep.GetListAsync();
            return r.ToList();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<R> DeleteAsync(long id)
        {
            if (await _ItemsDataRep.RecordCountAsync(new { ParentId = id }) > 0)
            {
                return R.Err(msg: "含有子数据不能删除");
            }

         
          
                bool b = await _ItemsDataRep.DeleteAsync(id);
                if (!b)
                {
                    return R.Err();
                }
              
              
          
            await RemoveCacheAsync();
            return R.Suc();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public async Task<ItemsData> GetAsync(long key)
        {
            ItemsData m = await _ItemsDataRep.GetAsync(key);
            return m;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="moduleEntity"></param>
        /// <returns></returns>
        public async Task<R> CreateAsync(ItemsData moduleEntity)
        {
            moduleEntity.Name = moduleEntity.Name?.Trim();
           
            moduleEntity.Remarks = moduleEntity.Remarks?.Trim();
            int count = await _ItemsDataRep.RecordCountAsync(new { Name = moduleEntity.Name , ParentId=moduleEntity .ParentId});
            if (count > 0)
            {
                return R.Err(msg: moduleEntity.Name + " 已存在");
            }

            int r = await _ItemsDataRep.InsertAsync(moduleEntity);
            if (r <= 0)
            {
                return R.Err(msg:"添加失败");
            }
            await RemoveCacheAsync();
            return R.Suc();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="moduleEntity"></param>
        /// <returns></returns>
        public async Task<R> UpdateAsync(ItemsData moduleEntity)
        {
            moduleEntity.Name = moduleEntity.Name?.Trim();
          
       
            moduleEntity.Remarks = moduleEntity.Remarks?.Trim();



            int count = await _ItemsDataRep.RecordCountAsync("where `ParentId`=@ParentId and  `Name`=@Name and `Id`<>@Id", new { Name = moduleEntity.Name, Id = moduleEntity.Id , ParentId= moduleEntity.ParentId });
            if (count > 0)
            {
                return R.Err(msg: moduleEntity.Name + " 已存在");
            }
            bool b = await _ItemsDataRep.UpdateAsync(moduleEntity);
            if (!b)
                return R.Err(msg:"编辑失败");

            await RemoveCacheAsync();
            return R.Suc();
        }


        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <returns></returns>
        private async Task RemoveCacheAsync()
        {
            //var cache = CacheFactory.Cache();
            //await cache.RemoveAsync("agent", "ItemsData");
          
        }
    }
}
